package com.maltys.monotonic.stack;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @author maltys
 * @link <a href="https://leetcode.com/problems/daily-temperatures/">每日温度</a>
 */
public class DailyTemperatures {
    public static void main(String[] args) {
        DailyTemperatures temperatures = new DailyTemperatures();
        int[] dailyTemperatures = temperatures.dailyTemperatures(new int[]{30, 60, 90});
        System.out.println(Arrays.toString(dailyTemperatures));
    }


    public int[] dailyTemperatures(int[] temperatures) {
        LinkedList<Integer> stack = new LinkedList<>();
        int[] res = new int[temperatures.length];
        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                Integer idx = stack.pop();
                res[idx] = i - idx;
            }
            stack.push(i);
        }
        return res;
    }
}
